package com.example.agendamesquita;


import java.util.ArrayList;
import java.util.HashMap;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;


public class HelperDB extends SQLiteOpenHelper {
	public HelperDB(Context context)
	{
		super(context, "List", null, 1);
		
	}
	
	@Override
	public void onCreate(SQLiteDatabase db)
	{
		db.execSQL("CREATE TABLE Evento(_id INTEGER PRIMARY KEY AUTOINCREMENT, nome varchar(40) NOT NULL, datahora varchar(40) NOT NULL, local varchar(40), descricao varchar(40));");
		//db.execSQL("CREATE TABLE Cadastro(id INTEGER PRIMARY KEY AUTOINCREMENT, nome varchar(40) NOT NULL, datanascimento char(10) NOT NULL, cpf char(14) NOT NULL, telefone varchar(9) NOT NULL);");
	}
	
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
	{
	
	}

	
	public void InserirEvento(HashMap<String, String> adicionaEvento)
	{
		
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues valores = new ContentValues();
		valores.put("nome" , adicionaEvento.get("nome"));
		valores.put("datahora",adicionaEvento.get("datahora"));
		valores.put("local", adicionaEvento.get("local"));
		valores.put("descricao", adicionaEvento.get("descricao"));
		db.insert("Evento", null , valores);
		db.close();
		
	}

	public int atualizarEvento(HashMap<String, String> AtualizaEvento)
	{
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues valores = new ContentValues();
		valores.put("nome", AtualizaEvento.get("nome"));
		valores.put("datahora", AtualizaEvento.get("datahora"));
		valores.put("local", AtualizaEvento.get("local"));
		valores.put("descricao", AtualizaEvento.get("descricao"));
		return db.update("Evento",valores, "_id" + " = ? ",new String[]{AtualizaEvento.get("_id")});
		
	}
	
	
	public void deletarEvento(String deletaId)
	{
	
	int id = Integer.parseInt(deletaId);
	SQLiteDatabase db = this.getWritableDatabase();
	String sql = "DELETE FROM Evento WHERE _id = "+ id ;
	db.execSQL(sql);	
		
	}
	
	
	public ArrayList<HashMap<String,String>> getTodosEventos()
	{
		
		ArrayList<HashMap<String,String>> getTodosEventos = new ArrayList<HashMap<String,String>>();
		String sql = "SELECT * FROM Evento";
		SQLiteDatabase db = this.getWritableDatabase();
		Cursor cursor = db.rawQuery(sql, null);
		if(cursor.moveToFirst()){
			
			do{
				
				HashMap<String,String> mapaEvento = new HashMap<String,String>();
				mapaEvento.put("_id", cursor.getString(0));
				mapaEvento.put("nome", cursor.getString(1));
				mapaEvento.put("datahora", cursor.getString(2));
				mapaEvento.put("local", cursor.getString(3));
				mapaEvento.put("descricao", cursor.getString(4));
				getTodosEventos.add(mapaEvento);
				
				
			}while(cursor.moveToNext());
			
			
			
		}
		
		return getTodosEventos;
	}
	
	
	

	

}